using System;
using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json;

namespace WINPE_TOOLBOX
{
    // 只读配置类
    public static class ReadOnlyConfig
    {
        public static readonly string Id = "240201";
        public static readonly string ClientVer = "V0.3.240201";
        public static readonly UrlConfig Url = new UrlConfig();
        public static readonly ResourcePathConfig resourcePath = new ResourcePathConfig();
        public static readonly EnvironmentConfig Environment1 = new EnvironmentConfig();

        public class UrlConfig
        {
            public string Home = "https://www.douyin.com/user/MS4wLjABAAAApqiVtWH6slS__FaraZdDMwn_RMapMHihPYZZsBw63jA";
            public string Github = "https://github.com/VirtualHotBar/HotPE_Client";
            public string Docs = "https://docs.hotpe.top/";
            public string Blog = "https://blog.hotpe.top/";
            public string Donate = "https://www.hotpe.top/donation/";
            public string Update = "API/HotPE/GetUpdate/";
            public string BtwiseUpdate = "HotPE/GetUpdate/";
            public PackageConfig Package = new PackageConfig();
        }

        public class PackageConfig
        {
            // public string PE = "Package/PE/{id}.7z";
            // public string Client = "Package/Client/{id}.7z";
        }


        public class ResourcePathConfig
        {
            public string Tools = ".\\resources\\tools\\";
            public string ClientTemp = ".\\resources\\temp\\";
            public string Pe = ".\\resources\\files\\pe\\";
            public string Client = ".\\resources\\files\\client\\";
            public string InstallTemp = ".\\resources\\temp\\install\\peFiles\\EFI\\HotPE\\";
        }

        public class EnvironmentConfig
        {
            public string SysLetter = RunCmdSync("echo %SystemDrive%").Substring(0, 2);
            public string Temp = RunCmdSync("echo %temp%").Replace("\r\n", "") + "\\";
            public string UserName = RunCmdSync("echo %UserName%").Replace("\r\n", "");
            public string DesktopDir = RunCmdSync("echo %SystemDrive%\\Users\\%UserName%\\Desktop\\").Replace("\r\n", "");
        }

        private static string RunCmdSync(string command)
        {
            // 实现命令执行逻辑
            return "";
        }
    }

    // 动态配置管理类
    public class ConfigManager
    {
        private const string ConfigPath = "./resources/config.json";
        public Config Config { get; private set; }

        public ConfigManager()
        {
            Config = Config.Instance; // 使用单例实例

            if (File.Exists(ConfigPath) && IsJson(File.ReadAllText(ConfigPath)))
            {
                // 如果配置文件存在且为JSON，则读取配置
                var loadedConfig = JsonConvert.DeserializeObject<Config>(File.ReadAllText(ConfigPath));
                if (loadedConfig != null)
                {
                    Config = loadedConfig;
                }
            }
            else
            {
                // 如果配置文件不存在，则使用默认配置
                SetDefaultConfig();
                SaveConfig();
            }
        }

        private void SetDefaultConfig()
        {
            Config.Api = new ApiConfig
            {
                Api = "https://api.hotpe.top/",
                Btwiseapi = "https://api.it0472.top/",
                Ghapi = "http://ghapi.hotpe.top/",
                Dl = "http://p0.hotpe.top/"
            };
            Config.State = new StateConfig
            {
                Install = "noDown",
                ResUpdate = "without",
                SetupToSys = "without"
            };
            Config.Environment = new EnvironmentConfig
            {
                HotPEDrive = new HotPEDriveConfig
                {
                    New = new NewDriveConfig { DiskIndex = -1, Letter = "", IsMove = false, Version = "" },
                    All = new List<string>()
                },
                Ware = new WareConfig
                {
                    System = new SystemConfig
                    {
                        Os = "",
                        BuildNumber = "",
                        UserName = "",
                        Architecture = "",
                        Firmware = ""
                    },
                    Disks = new List<DisksInfo>(),
                    Partitions = new List<PartitionInfo>(),
                    AllLetter = new List<string>()
                }
            };
            Config.Resources = new ResourcesConfig
            {
                Pe = new PeConfig
                {
                    New = "",
                    All = new List<string>(),
                    LocalWim = new List<string>(),
                    Update = new UpdateLatest
                    {
                        //Id = "",
                        //Name = "",
                        //PushTime = "",
                        //Body = "",
                        //Size = 0,
                        //DownloadUrl = "",
                        //DownloadUrlGithub = "",
                        //FileName = ""
                    }
                },
                Client = new ClientConfig
                {
                    New = "",
                    All = new List<object>(),
                    Update = new UpdateLatest
                    {
                        //Id = "",
                        //Name = "",
                        //PushTime = "",
                        //Body = "",
                        //Size = 0,
                        //DownloadUrl = "",
                        //DownloadUrlGithub = "",
                        //FileName = ""
                    }
                }
            };
            Config.Notice = new NoticeConfig
            {
                Show = false,
                Type = "info",
                Content = ""
            };
            Config.Download = new DownloadConfig
            {
                Thread = 16
            };
            Config.Setting = new SettingConfig
            {
                Pe = new PeSettingConfig { BootWaitTime = 3 },
                Client = new ClientSettingConfig { ThemeMode = "auto" }
            };
        }

        private bool IsJson(string input)
        {
            // Implement JSON validation logic here
            return true;
        }

        public void SaveConfig()
        {
            File.WriteAllText(ConfigPath, JsonConvert.SerializeObject(Config, Formatting.Indented));
        }
    }
}
